package com.perdragon.controller;

import java.text.ParseException;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.perdragon.entity.Institution;
import com.perdragon.service.InstitutionService;

@Controller
@RequestMapping("/institution")
public class InstitutionController {
	@Autowired
	private InstitutionService institutionService;

	@RequestMapping("/list")
	public ModelAndView listInstitution(String page) {
		int pageNum = 1;
		if (page != null)
			pageNum = Integer.parseInt(page);
		int rowNum = institutionService.getRowNum();
		int maxPageNum = (int) Math.ceil(rowNum / 10.0);
		if (maxPageNum == 0)
			maxPageNum = 1;
		if (pageNum >= 1 && pageNum <= maxPageNum) {
			ModelAndView mv = new ModelAndView();
			mv.setViewName("institution/listInstitution");
			mv.addObject("institutions", institutionService.listInstitution((pageNum - 1) * 10));
			mv.addObject("pageNum", pageNum);
			mv.addObject("maxPageNum", maxPageNum);
			return mv;
		} else {
			ModelAndView mv = new ModelAndView("redirect:/institution/list");
			return mv;
		}
	}

	@GetMapping("/update")
	public ModelAndView updateInstitution(int id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("institution/updateInstitution");
		mv.addObject("institution", institutionService.getInstitution(id));
		return mv;
	}

	@PostMapping("/update")
	public ModelAndView doUpdateInstitution(int id, String name, String reason, String explains) {
		Institution institution = new Institution();
		institution.setId(id);
		institution.setName(name);
		institution.setReason(reason);
		institution.setExplains(explains);
		Date createtime = new Date(System.currentTimeMillis());
		institution.setCreatetime(createtime);
		institutionService.updateInstitution(institution);
		ModelAndView mv = new ModelAndView("redirect:/institution/list");
		return mv;
	}

	@RequestMapping("/delete")
	public ModelAndView deleteInstitution(int id) {
		institutionService.deleteInstitution(id);
		ModelAndView mv = new ModelAndView("redirect:/institution/list");
		return mv;
	}

	@RequestMapping("/detail")
	public ModelAndView showDetail(int id) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("institution/detailInstitution");
		mv.addObject("institutions", institutionService.showDetail(id));
		return mv;
	}

	@GetMapping("/add")
	public ModelAndView addInstitution() {
		ModelAndView mv = new ModelAndView("institution/addInstitution");
		return mv;
	}

	@PostMapping("/add")
	public ModelAndView doAddInstitution(String name, String reason, String explains) throws ParseException {
		Institution institution = new Institution();
		institution.setName(name);
		institution.setReason(reason);
		institution.setExplains(explains);
		Date createtime = new Date(System.currentTimeMillis());
		institution.setCreatetime(createtime);
		System.out.println(institution);
		institutionService.addInstitution(institution);
		ModelAndView mv = new ModelAndView("redirect:/institution/list");
		return mv;
	}
}
